package org.jctc.els.modules.biz.service.projects.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.units.qual.A;
import org.jctc.els.common.constant.BaseRoleConstant;
import org.jctc.els.modules.biz.entity.DictBiz;
import org.jctc.els.modules.biz.entity.pro.ElsProjectReview;
import org.jctc.els.modules.biz.entity.projects.ElsProjectFiles;
import org.jctc.els.modules.biz.entity.projects.ElsProjectFilesCategory;
import org.jctc.els.modules.biz.entity.projects.ElsProjects;
import org.jctc.els.modules.biz.entity.projects.ElsProjectsStatusLog;
import org.jctc.els.modules.biz.entity.user.User;
import org.jctc.els.modules.biz.excel.*;
import org.jctc.els.modules.biz.mapper.DictBizMapper;
import org.jctc.els.modules.biz.mapper.pro.ElsProjectReviewMapper;
import org.jctc.els.modules.biz.mapper.projects.ElsProjectFilesCategoryMapper;
import org.jctc.els.modules.biz.mapper.projects.ElsProjectFilesMapper;
import org.jctc.els.modules.biz.mapper.projects.ElsProjectsMapper;
import org.jctc.els.modules.biz.mapper.projects.ElsProjectsStatusLogMapper;
import org.jctc.els.modules.biz.mapper.user.UserMapper;
import org.jctc.els.modules.biz.service.projects.ElsProjectsService;
import org.jctc.els.modules.biz.vo.ElsProjectFilesVO;
import org.jctc.els.modules.biz.vo.ElsProjectTeamAuditVO;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目表;(els_projects)表服务实现类
 * @author : Ice
 * @date : 2022-6-21
 */
@Service
public class ElsProjectsServiceImpl extends BaseServiceImpl<ElsProjectsMapper,ElsProjects> implements ElsProjectsService {
    @Autowired
    private ElsProjectsMapper elsProjectMapper;

    @Autowired
    private ElsProjectFilesMapper elsProjectFilesMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ElsProjectFilesCategoryMapper elsProjectFilesCategoryMapper;

    @Autowired
    private ElsProjectsStatusLogMapper elsProjectsStatusLogMapper;

    @Autowired
    private ElsProjectReviewMapper elsProjectReviewMapper;

    @Autowired
    private DictBizMapper dictBizMapper;
    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return 查询结果
     */
    @Override
    public IPage<ElsProjects> paginQuery(Integer pageNum, Integer pageSize,String projectName) {
        Page<ElsProjects> elsProjectsPage = new Page<>(pageNum, pageSize);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        Page<ElsProjects> page = baseMapper.paginQuery(elsProjectsPage,projectName,year);
        return page;
    }

    @Override
    public IPage<ElsProjectFilesVO> paginCategoryQuery(Integer pageNum, Integer pageSize, Integer projectCategory, Integer projectType, String projectName) {
        Page<ElsProjects> elsProjectsPage = new Page<>(pageNum, pageSize);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        Page<ElsProjectFilesVO> page = baseMapper.paginCategoryQuery(elsProjectsPage,projectCategory,projectType,projectName,year);
        List<ElsProjectFilesVO> records = page.getRecords();
        for (ElsProjectFilesVO record : records) {

            //审核状态
            List<ElsProjectsStatusLog> elsProjectsStatusLogs = elsProjectsStatusLogMapper.selectList(new QueryWrapper<ElsProjectsStatusLog>().eq("project_id", record.getId()).orderByDesc("auth_time"));
            if (elsProjectsStatusLogs.size() == 0) {
                record.setAuditStatusStr("未提交");
            } else {
                ElsProjectsStatusLog elsProjectsStatusLog = elsProjectsStatusLogs.get(0);
                //地责专员
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_SPECIAL_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 2 :
                            record.setAuditStatusStr("待审核");
                            break;
                    }
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
                //地市管理员
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_SPECIAL_ADMIN_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 0 :
                            record.setAuditStatusStr("地市管理员审核拒绝");
                            break;
                        case 1 :
                            record.setAuditStatusStr("地市管理员审核通过");
                            break;
                        case 2 :
                            record.setAuditStatusStr("待省公司审核");
                            break;
                    }
                    record.setStatus(elsProjectsStatusLog.getStatus());
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
                //省公司
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_ADMIN_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 0 :
                            record.setAuditStatusStr("省公司审核拒绝");
                            break;
                        case 1 :
                            record.setAuditStatusStr("审核通过");
                            break;
                    }
                    record.setStatus(elsProjectsStatusLog.getStatus());
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
            }
        }
        page.setRecords(records);
        return page;
    }

    @Override
    public IPage<ElsProjectFilesVO> paginCategoryQueryByExpert(Integer pageNum, Integer pageSize, Integer projectCategory, Integer projectType, String projectName,Long expertId) {
        Page<ElsProjects> elsProjectsPage = new Page<>(pageNum, pageSize);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        Page<ElsProjectFilesVO> page = baseMapper.paginCategoryQueryByExpert(elsProjectsPage,projectCategory,projectType,projectName,year,expertId);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R importWorkersProjects(MultipartFile multipartFile, HttpServletRequest request, Integer projectCategory, Integer projectType, BladeUser user) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        //根据地址和文件名准确获取用户上传的表格
        File tempFile = new File(filePath + multipartFile.getOriginalFilename());
        List<WorkersProjectsExcel> data = ExcelUtil.read(multipartFile, WorkersProjectsExcel.class);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        String resultMsg = "";
        for (int i = 0; i < data.size(); i++) {
            WorkersProjectsExcel projectsExcel = data.get(i);
            String costType = projectsExcel.getCostType();
            String category1 = projectsExcel.getCategory1();
            if(StrUtil.isEmpty(category1)){
                throw new RuntimeException("一级分类不能为空！");
            }
            if (costType != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", costType));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("cost_type");
                    dictBiz.setDictKey("cost_type");
                    dictBiz.setDictValue(costType);
                    dictBiz.setRemark(costType);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            if (category1 != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category1));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("one_classify");
                    dictBiz.setDictKey("one_classify");
                    dictBiz.setDictValue(category1);
                    dictBiz.setRemark(category1);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category2 = projectsExcel.getCategory2();
            if (category2 != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category2));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("two_classify");
                    dictBiz.setDictKey("two_classify");
                    dictBiz.setDictValue(category2);
                    dictBiz.setRemark(category2);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category3 = projectsExcel.getCategory3();
            if (category3 != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category3));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("three_classify");
                    dictBiz.setDictKey("three_classify");
                    dictBiz.setDictValue(category3);
                    dictBiz.setRemark(category3);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String trainingType = projectsExcel.getTrainingType();
            if (trainingType != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", trainingType));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("training_type");
                    dictBiz.setDictKey("training_type");
                    dictBiz.setDictValue(trainingType);
                    dictBiz.setRemark(trainingType);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String trainingPlace = projectsExcel.getTrainingPlace();
            if (trainingPlace != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", trainingPlace));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("training_place");
                    dictBiz.setDictKey("training_place");
                    dictBiz.setDictValue(trainingPlace);
                    dictBiz.setRemark(trainingPlace);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String trainingCatetory = projectsExcel.getTrainingCategory();
            if (trainingCatetory != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", trainingCatetory));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("training_category");
                    dictBiz.setDictKey("training_category");
                    dictBiz.setDictValue(trainingCatetory);
                    dictBiz.setRemark(trainingCatetory);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String specializedCategory = projectsExcel.getSpecializedCategory();
            if (specializedCategory != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", specializedCategory));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("specialized_category");
                    dictBiz.setDictKey("specialized_category");
                    dictBiz.setDictValue(specializedCategory);
                    dictBiz.setRemark(specializedCategory);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            ElsProjects elsProjects = Objects.requireNonNull(BeanUtil.copy(projectsExcel, ElsProjects.class));
            elsProjects.setProjectCategory(projectCategory);
            elsProjects.setProjectType(projectType);
            elsProjects.setYear(year);
            elsProjects.setUpdatedTime(DateUtil.now());
            elsProjects.setCreatedTime(DateUtil.now());
            elsProjects.setDeptId(Long.valueOf(user.getDeptId()));
            Long baseRole = user.getBaseRole();
            if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ROLE)) {
                elsProjects.setAuditStatus(0);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ADMIN_ROLE)) {
                elsProjects.setAuditStatus(2);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMIN_ROLE) || BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMINISTRATOR_ROLE)) {
                elsProjects.setAuditStatus(4);
            }
            try {
                this.save(elsProjects);
            } catch (DuplicateKeyException exception) {
                resultMsg += StrUtil.format("项目编码【{}】、项目名称【{}】重复，第【{}】行。<br>", elsProjects.getProjectNumber(), elsProjects.getProjectName(), i + 2);
            }
        }
        return StringUtil.isNotBlank(resultMsg) ? R.fail(resultMsg) : R.success("");
    }

    @Override
    public R importPersonnelProjects(MultipartFile multipartFile, HttpServletRequest request, Integer projectCategory,Integer projectType,BladeUser user) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        //根据地址和文件名准确获取用户上传的表格
        File tempFile = new File(filePath + multipartFile.getOriginalFilename());
        List<PersonnelProjectsExcel> data = ExcelUtil.read(multipartFile, PersonnelProjectsExcel.class);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        String resultMsg = "";
        for (int i = 0; i < data.size(); i++) {
            PersonnelProjectsExcel projectsExcel = data.get(i);
            String category1 = projectsExcel.getCategory1();
            if(StrUtil.isEmpty(category1)){
                throw new RuntimeException("一级分类不能为空！");
            }
            if (category1 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category1));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("one_classify");
                    dictBiz.setDictKey("one_classify");
                    dictBiz.setDictValue(category1);
                    dictBiz.setRemark(category1);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category2 = projectsExcel.getCategory2();
            if (category2 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category2));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("two_classify");
                    dictBiz.setDictKey("two_classify");
                    dictBiz.setDictValue(category2);
                    dictBiz.setRemark(category2);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category3 = projectsExcel.getCategory3();
            if (category3 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category3));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("three_classify");
                    dictBiz.setDictKey("three_classify");
                    dictBiz.setDictValue(category3);
                    dictBiz.setRemark(category3);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String trainingCatetory = projectsExcel.getTrainingCategory();
            if (trainingCatetory !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", trainingCatetory));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("training_category");
                    dictBiz.setDictKey("training_category");
                    dictBiz.setDictValue(trainingCatetory);
                    dictBiz.setRemark(trainingCatetory);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String trainingType = projectsExcel.getTrainingType();
            if (trainingType !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", trainingType));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("training_type");
                    dictBiz.setDictKey("training_type");
                    dictBiz.setDictValue(trainingType);
                    dictBiz.setRemark(trainingType);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String specializedCategory = projectsExcel.getSpecializedCategory();
            if (specializedCategory !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", specializedCategory));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("specialized_category");
                    dictBiz.setDictKey("specialized_category");
                    dictBiz.setDictValue(specializedCategory);
                    dictBiz.setRemark(specializedCategory);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String costType = projectsExcel.getCostType();
            if (costType !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", costType));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("cost_type");
                    dictBiz.setDictKey("cost_type");
                    dictBiz.setDictValue(costType);
                    dictBiz.setRemark(costType);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String trainingPlace = projectsExcel.getTrainingPlace();
            if (trainingPlace !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", trainingPlace));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("training_place");
                    dictBiz.setDictKey("training_place");
                    dictBiz.setDictValue(trainingPlace);
                    dictBiz.setRemark(trainingPlace);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            ElsProjects elsProjects = Objects.requireNonNull(BeanUtil.copy(projectsExcel, ElsProjects.class));
            elsProjects.setProjectCategory(projectCategory);
            elsProjects.setProjectType(projectType);
            elsProjects.setYear(year);
            elsProjects.setUpdatedTime(DateUtil.now());
            elsProjects.setCreatedTime(DateUtil.now());
            elsProjects.setDeptId(Long.valueOf(user.getDeptId()));
            Long baseRole = user.getBaseRole();
            if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ROLE)) {
                elsProjects.setAuditStatus(0);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ADMIN_ROLE)) {
                elsProjects.setAuditStatus(2);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMIN_ROLE) || BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMINISTRATOR_ROLE)) {
                elsProjects.setAuditStatus(4);
            }
            try {
                this.save(elsProjects);
            }catch(DuplicateKeyException exception){
                resultMsg += StrUtil.format("项目编码【{}】、项目名称【{}】重复，第【{}】行。<br>",elsProjects.getProjectNumber(),elsProjects.getProjectName(),i+2) ;
            }

        }

        return StringUtil.isNotBlank(resultMsg) ? R.fail(resultMsg) : R.success("");
    }

    @Override
    public R importExploitProjects(MultipartFile multipartFile, HttpServletRequest request, Integer projectCategory,Integer projectType,BladeUser user) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        //根据地址和文件名准确获取用户上传的表格
        File tempFile = new File(filePath + multipartFile.getOriginalFilename());
        List<ExploitProjectsExcel> data = ExcelUtil.read(multipartFile, ExploitProjectsExcel.class);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        String resultMsg = "";
        for (int i = 0; i < data.size(); i++) {
            ExploitProjectsExcel projectsExcel = data.get(i);
            String category1 = projectsExcel.getCategory1();
            if(StrUtil.isEmpty(category1)){
                throw new RuntimeException("一级分类不能为空！");
            }
            if (category1 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category1));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("one_classify");
                    dictBiz.setDictKey("one_classify");
                    dictBiz.setDictValue(category1);
                    dictBiz.setRemark(category1);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category2 = projectsExcel.getCategory2();
            if (category2 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category2));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("two_classify");
                    dictBiz.setDictKey("two_classify");
                    dictBiz.setDictValue(category2);
                    dictBiz.setRemark(category2);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category3 = projectsExcel.getCategory3();
            if (category3 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category3));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("three_classify");
                    dictBiz.setDictKey("three_classify");
                    dictBiz.setDictValue(category3);
                    dictBiz.setRemark(category3);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String costType = projectsExcel.getCostType();
            if (costType !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", costType));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("cost_type");
                    dictBiz.setDictKey("cost_type");
                    dictBiz.setDictValue(costType);
                    dictBiz.setRemark(costType);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            ElsProjects elsProjects = Objects.requireNonNull(BeanUtil.copy(projectsExcel, ElsProjects.class));
            elsProjects.setProjectCategory(projectCategory);
            elsProjects.setProjectType(projectType);
            elsProjects.setYear(year);
            elsProjects.setUpdatedTime(DateUtil.now());
            elsProjects.setCreatedTime(DateUtil.now());
            elsProjects.setDeptId(Long.valueOf(user.getDeptId()));
            Long baseRole = user.getBaseRole();
            if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ROLE)) {
                elsProjects.setAuditStatus(0);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ADMIN_ROLE)) {
                elsProjects.setAuditStatus(2);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMIN_ROLE) || BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMINISTRATOR_ROLE)) {
                elsProjects.setAuditStatus(4);
            }
            try {
                this.save(elsProjects);
            }catch(DuplicateKeyException exception){
                resultMsg += StrUtil.format("项目编码【{}】、项目名称【{}】重复，第【{}】行。<br>",elsProjects.getProjectNumber(),elsProjects.getProjectName(),i+2) ;
            }
        }
        return StringUtil.isNotBlank(resultMsg) ? R.fail(resultMsg) : R.success("");
    }

    @Override
    public R importPurchaseProjects(MultipartFile multipartFile, HttpServletRequest request, Integer projectCategory,Integer projectType,BladeUser user) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        //根据地址和文件名准确获取用户上传的表格
        File tempFile = new File(filePath + multipartFile.getOriginalFilename());
        List<PurchaseProjectsExcel> data = ExcelUtil.read(multipartFile, PurchaseProjectsExcel.class);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        String resultMsg = "";
        for (int i = 0; i < data.size(); i++) {
            PurchaseProjectsExcel projectsExcel = data.get(i);
            String category1 = projectsExcel.getCategory1();
            if(StrUtil.isEmpty(category1)){
                throw new RuntimeException("一级分类不能为空！");
            }
            if (category1 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category1));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("one_classify");
                    dictBiz.setDictKey("one_classify");
                    dictBiz.setDictValue(category1);
                    dictBiz.setRemark(category1);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category2 = projectsExcel.getCategory2();
            if (category2 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category2));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("two_classify");
                    dictBiz.setDictKey("two_classify");
                    dictBiz.setDictValue(category2);
                    dictBiz.setRemark(category2);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category3 = projectsExcel.getCategory3();
            if (category3 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category3));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("three_classify");
                    dictBiz.setDictKey("three_classify");
                    dictBiz.setDictValue(category3);
                    dictBiz.setRemark(category3);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String costType = projectsExcel.getCostType();
            if (costType !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", costType));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("cost_type");
                    dictBiz.setDictKey("cost_type");
                    dictBiz.setDictValue(costType);
                    dictBiz.setRemark(costType);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            ElsProjects elsProjects = Objects.requireNonNull(BeanUtil.copy(projectsExcel, ElsProjects.class));
            elsProjects.setProjectCategory(projectCategory);
            elsProjects.setProjectType(projectType);
            elsProjects.setYear(year);
            elsProjects.setUpdatedTime(DateUtil.now());
            elsProjects.setCreatedTime(DateUtil.now());
            elsProjects.setDeptId(Long.valueOf(user.getDeptId()));
            Long baseRole = user.getBaseRole();
            if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ROLE)) {
                elsProjects.setAuditStatus(0);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ADMIN_ROLE)) {
                elsProjects.setAuditStatus(2);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMIN_ROLE) || BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMINISTRATOR_ROLE)) {
                elsProjects.setAuditStatus(4);
            }
            try {
                this.save(elsProjects);
            }catch(DuplicateKeyException exception){
                resultMsg += StrUtil.format("项目编码【{}】、项目名称【{}】重复，第【{}】行。<br>",elsProjects.getProjectNumber(),elsProjects.getProjectName(),i+2) ;
            }
        }
        return StringUtil.isNotBlank(resultMsg) ? R.fail(resultMsg) : R.success("");
    }

    @Override
    public R importProduceProjects(MultipartFile multipartFile, HttpServletRequest request, Integer projectCategory,Integer projectType,BladeUser user) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        //根据地址和文件名准确获取用户上传的表格
        File tempFile = new File(filePath + multipartFile.getOriginalFilename());
        List<ProduceProjectsExcel> data = ExcelUtil.read(multipartFile, ProduceProjectsExcel.class);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        String resultMsg = "";
        for (int i = 0; i < data.size(); i++) {
            ProduceProjectsExcel projectsExcel = data.get(i);
            String category1 = projectsExcel.getCategory1();
            if(StrUtil.isEmpty(category1)){
                throw new RuntimeException("一级分类不能为空！");
            }
            if (category1 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category1));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("one_classify");
                    dictBiz.setDictKey("one_classify");
                    dictBiz.setDictValue(category1);
                    dictBiz.setRemark(category1);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category2 = projectsExcel.getCategory2();
            if (category2 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category2));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("two_classify");
                    dictBiz.setDictKey("two_classify");
                    dictBiz.setDictValue(category2);
                    dictBiz.setRemark(category2);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String category3 = projectsExcel.getCategory3();
            if (category3 !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category3));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("three_classify");
                    dictBiz.setDictKey("three_classify");
                    dictBiz.setDictValue(category3);
                    dictBiz.setRemark(category3);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String purchaseCategory = projectsExcel.getPurchaseCategory();
            if (purchaseCategory !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", purchaseCategory));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("purchase_category");
                    dictBiz.setDictKey("purchase_category");
                    dictBiz.setDictValue(purchaseCategory);
                    dictBiz.setRemark(purchaseCategory);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String costType = projectsExcel.getCostType();
            if (costType !=null){
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", costType));
                if (dictValue.size()<=0){
                    DictBiz dictBiz=new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("cost_type");
                    dictBiz.setDictKey("cost_type");
                    dictBiz.setDictValue(costType);
                    dictBiz.setRemark(costType);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            String specializedCategory = projectsExcel.getSpecializedCategory();
            if (specializedCategory != null) {
                List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", specializedCategory));
                if (dictValue.size() <= 0) {
                    DictBiz dictBiz = new DictBiz();
                    dictBiz.setTenantId("000000");
                    dictBiz.setCode("specialized_category");
                    dictBiz.setDictKey("specialized_category");
                    dictBiz.setDictValue(specializedCategory);
                    dictBiz.setRemark(specializedCategory);
                    dictBiz.setCreatedTime(new Date());
                    dictBizMapper.insert(dictBiz);
                }
            }
            ElsProjects elsProjects = Objects.requireNonNull(BeanUtil.copy(projectsExcel, ElsProjects.class));
            elsProjects.setProjectCategory(projectCategory);
            elsProjects.setProjectType(projectType);
            elsProjects.setYear(year);
            elsProjects.setUpdatedTime(DateUtil.now());
            elsProjects.setCreatedTime(DateUtil.now());
            elsProjects.setDeptId(Long.valueOf(user.getDeptId()));
            Long baseRole = user.getBaseRole();
            if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ROLE)) {
                elsProjects.setAuditStatus(0);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_SPECIAL_ADMIN_ROLE)) {
                elsProjects.setAuditStatus(2);
            } else if (BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMIN_ROLE) || BaseRoleUtil.hasRole(baseRole, BaseRoleConstant.BASE_ADMINISTRATOR_ROLE)) {
                elsProjects.setAuditStatus(4);
            }
            try {
                this.save(elsProjects);
            }catch(DuplicateKeyException exception){
                resultMsg += StrUtil.format("项目编码【{}】、项目名称【{}】重复，第【{}】行。<br>",elsProjects.getProjectNumber(),elsProjects.getProjectName(),i+2) ;
            }
        }
        return StringUtil.isNotBlank(resultMsg) ? R.fail(resultMsg) : R.success("");
    }

    @Override
    public R importScienceProjects(MultipartFile multipartFile, HttpServletRequest request, Integer projectType) {
        String filePath = request.getSession().getServletContext().getRealPath("/");
        //根据地址和文件名准确获取用户上传的表格
        File tempFile = new File(filePath + multipartFile.getOriginalFilename());
        List<ExploitProjectsExcel> data = ExcelUtil.read(multipartFile, ExploitProjectsExcel.class);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        String resultMsg = "";
        for (int i = 0; i < data.size(); i++) {
            ExploitProjectsExcel projectsExcel = data.get(i);
            String category1 = projectsExcel.getCategory1();
                if (category1 !=null){
                    List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category1));
                    if (dictValue.size()<=0){
                        DictBiz dictBiz=new DictBiz();
                        dictBiz.setTenantId("000000");
                        dictBiz.setCode("one_classify");
                        dictBiz.setDictKey("one_classify");
                        dictBiz.setDictValue(category1);
                        dictBiz.setRemark(category1);
                        dictBiz.setCreatedTime(new Date());
                        dictBizMapper.insert(dictBiz);
                    }
                }
                String category2 = projectsExcel.getCategory2();
                if (category2 !=null){
                    List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category2));
                    if (dictValue.size()<=0){
                        DictBiz dictBiz=new DictBiz();
                        dictBiz.setTenantId("000000");
                        dictBiz.setCode("two_classify");
                        dictBiz.setDictKey("two_classify");
                        dictBiz.setDictValue(category2);
                        dictBiz.setRemark(category2);
                        dictBiz.setCreatedTime(new Date());
                        dictBizMapper.insert(dictBiz);
                    }
                }
                String category3 = projectsExcel.getCategory3();
                if (category3 !=null){
                    List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", category3));
                    if (dictValue.size()<=0){
                        DictBiz dictBiz=new DictBiz();
                        dictBiz.setTenantId("000000");
                        dictBiz.setCode("three_classify");
                        dictBiz.setDictKey("three_classify");
                        dictBiz.setDictValue(category3);
                        dictBiz.setRemark(category3);
                        dictBiz.setCreatedTime(new Date());
                        dictBizMapper.insert(dictBiz);
                    }
                }
                String costType = projectsExcel.getCostType();
                if (costType !=null){
                    List<DictBiz> dictValue = dictBizMapper.selectList(new QueryWrapper<DictBiz>().eq("dict_value", costType));
                    if (dictValue.size()<=0){
                        DictBiz dictBiz=new DictBiz();
                        dictBiz.setTenantId("000000");
                        dictBiz.setCode("cost_type");
                        dictBiz.setDictKey("cost_type");
                        dictBiz.setDictValue(costType);
                        dictBiz.setRemark(costType);
                        dictBiz.setCreatedTime(new Date());
                        dictBizMapper.insert(dictBiz);
                    }
                }
                ElsProjects elsProjects = Objects.requireNonNull(BeanUtil.copy(projectsExcel, ElsProjects.class));
                elsProjects.setProjectType(projectType);
                elsProjects.setYear(year);
                elsProjects.setUpdatedTime(DateUtil.now());
                elsProjects.setCreatedTime(DateUtil.now());
                try {
                    this.save(elsProjects);
                }catch(DuplicateKeyException exception){
                    resultMsg += StrUtil.format("项目编码【{}】、项目名称【{}】重复，第【{}】行。<br>",elsProjects.getProjectNumber(),elsProjects.getProjectName(),i+2) ;
                }
        }
        return StringUtil.isNotBlank(resultMsg) ? R.fail(resultMsg) : R.success("");
    }

    @Override
    public List<WorkersProjectsExcel> exportWorkersProjects(Wrapper<ElsProjects> queryWrapper, Integer projectCategory,Integer projectType,Long expertId) {
        List<WorkersProjectsExcel> projectsExcelList = baseMapper.exportWorkersProjects(queryWrapper,projectCategory,projectType,expertId);
        return projectsExcelList;
    }
    @Override
    public List<PersonnelProjectsExcel> exportPersonnelProjects(Wrapper<ElsProjects> queryWrapper, Integer projectCategory,Integer projectType, Long expertId) {
        List<PersonnelProjectsExcel> projectsExcelList = baseMapper.exportPersonnelProjects(queryWrapper,projectCategory,projectType,expertId);
        return projectsExcelList;
    }
    @Override
    public List<ExploitProjectsExcel> exportExploitProjects(Wrapper<ElsProjects> queryWrapper, Integer projectCategory,Integer projectType, Long expertId) {
        List<ExploitProjectsExcel> projectsExcelList = baseMapper.exportExploitProjects(queryWrapper,projectCategory,projectType,expertId);
        return projectsExcelList;
    }
    @Override
    public List<PurchaseProjectsExcel> exportPurchaseProjects(Wrapper<ElsProjects> queryWrapper, Integer projectCategory,Integer projectType, Long expertId) {
        List<PurchaseProjectsExcel> projectsExcelList = baseMapper.exportPurchaseProjects(queryWrapper,projectCategory,projectType,expertId);
        return projectsExcelList;
    }
    @Override
    public List<ProduceProjectsExcel> exportProduceProjects(Wrapper<ElsProjects> queryWrapper,Integer projectCategory,Integer projectType, Long expertId) {
        List<ProduceProjectsExcel> projectsExcelList = baseMapper.exportProduceProjects(queryWrapper,projectCategory,projectType,expertId);
        return projectsExcelList;
    }

    @Override
    public List<ScienceProjectsExcel> exportScienceProjects(Wrapper<ElsProjects> queryWrapper, Integer projectType, Long expertId) {
        List<ScienceProjectsExcel> projectsExcelList = baseMapper.exportScienceProjects(queryWrapper,projectType,expertId);
        return projectsExcelList;
    }

    @Override
    public IPage<ElsProjects> selectProjects(IPage<ElsProjects> page, String projectNumber, String orgName, String projectName, String category1, String category2, String category3, String projectLevel, String trainingCategory, String trainingType, String trainingMode, String deptApply, String deptHost, String orgBy, String implementScope, String purchaseCategory, String specializedCategory, String openTime, String completionTime,Integer projectType) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        System.out.println("年份"+year);
        return page.setRecords(elsProjectMapper.selectProjects(page,year,projectNumber,
                orgName,
                projectName,
                category1,
                category2,
                category3,
                projectLevel,
                trainingCategory,
                trainingType,
                trainingMode,
                deptApply,
                deptHost,
                orgBy,
                implementScope,
                purchaseCategory,
                specializedCategory,
                openTime,
                completionTime,
                projectType
                ));
    }

    @Override
    public IPage<ElsProjects> selectBoundProjects(IPage<ElsProjects> page, String teamId, String projectNumber, String orgName, String projectName, String category1, String category2, String category3, String projectLevel, String trainingCategory, String trainingType, String trainingMode, String deptApply, String deptHost, String orgBy, String implementScope, String purchaseCategory, String specializedCategory, String openTime, String completionTime) {
        return page.setRecords(elsProjectMapper.selectBoundProjects(page,teamId,projectNumber,
                orgName,
                projectName,
                category1,
                category2,
                category3,
                projectLevel,
                trainingCategory,
                trainingType,
                trainingMode,
                deptApply,
                deptHost,
                orgBy,
                implementScope,
                purchaseCategory,
                specializedCategory,
                openTime,
                completionTime));

    }

    @Override
    public IPage<ElsProjectTeamAuditVO> paginQueryTeamAudit(Integer pageNum, Integer pageSize, String projectName, Long userId, Integer projectCategory,Long teamId,Integer status) {
        Page<ElsProjectTeamAuditVO> elsProjectsPage = new Page<>(pageNum, pageSize);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        Page<ElsProjectTeamAuditVO> page = baseMapper.paginQueryTeamAudit(elsProjectsPage,projectName,year,projectCategory, userId,teamId,status);
        List<ElsProjectTeamAuditVO> teamAuditList = page.getRecords();
        for (ElsProjectTeamAuditVO t : teamAuditList) {
            List<Double> finalScoreList = elsProjectReviewMapper.selectList(new QueryWrapper<ElsProjectReview>().eq("project_id", t.getId())).stream().map(ElsProjectReview::getFinalScore).collect(Collectors.toList());
            if (CollUtil.isEmpty(finalScoreList)) {
                t.setAverageScore(0.0);
                continue;
            }
            //求平均分
            double avg = finalScoreList.stream().mapToDouble(Double::doubleValue).average().getAsDouble();
            t.setAverageScore(avg);
        }
        page.setRecords(teamAuditList);
        return page;
    }


    @Override
    public IPage<ElsProjectTeamAuditVO> paginQueryAdminAudit(Integer pageNum, Integer pageSize, String projectName, Long userId, Integer projectCategory,Long teamId,Integer status) {
        Page<ElsProjectTeamAuditVO> elsProjectsPage = new Page<>(pageNum, pageSize);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        Page<ElsProjectTeamAuditVO> page = baseMapper.paginQueryAdminAudit(elsProjectsPage,projectName,year,projectCategory,teamId,status);
        List<ElsProjectTeamAuditVO> teamAuditList = page.getRecords();
        for (ElsProjectTeamAuditVO t : teamAuditList) {
            List<Double> finalScoreList = elsProjectReviewMapper.selectList(new QueryWrapper<ElsProjectReview>().eq("project_id", t.getId())).stream().map(ElsProjectReview::getFinalScore).collect(Collectors.toList());
            if (CollUtil.isEmpty(finalScoreList)) {
                t.setAverageScore(0.0);
                continue;
            }
            //求平均分
            double avg = finalScoreList.stream().mapToDouble(Double::doubleValue).average().getAsDouble();
            t.setAverageScore(avg);
        }
        page.setRecords(teamAuditList);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removes(List<Long> ids) {
        boolean result = true;
        if(Func.isNotEmpty(ids)){
            for (Long id:ids) {
                UpdateWrapper<ElsProjects> updateWrapper =  new UpdateWrapper<>();
                updateWrapper.set("is_deleted", 1).set("delete_time",new Date()).eq("id", id);
                result =  baseMapper.update(null,updateWrapper) > 0;
                if(BooleanUtil.isFalse(result)){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    break;
                }
            }
        }
        return result;
    }

    @Override
    public IPage<ElsProjectFilesVO> paginCategoryQueryBySpecial(Integer pageNum, Integer pageSize, Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Page<ElsProjects> elsProjectsPage = new Page<>(pageNum, pageSize);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        Page<ElsProjectFilesVO> page = baseMapper.paginCategoryQueryBySpecial(elsProjectsPage,projectCategory,projectType,projectName,year,userId);

        List<ElsProjectFilesVO> records = page.getRecords();
        for (ElsProjectFilesVO record : records) {

            //审核状态
            List<ElsProjectsStatusLog> elsProjectsStatusLogs = elsProjectsStatusLogMapper.selectList(new QueryWrapper<ElsProjectsStatusLog>().eq("project_id", record.getId()).orderByDesc("auth_time"));
            if (elsProjectsStatusLogs.size() == 0) {
                record.setAuditStatusStr("未提交");
            } else {
                ElsProjectsStatusLog elsProjectsStatusLog = elsProjectsStatusLogs.get(0);
                //地责专员
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_SPECIAL_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 2 :
                            record.setAuditStatusStr("待地市管理员审核");
                            break;
                    }
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
                //地市管理员
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_SPECIAL_ADMIN_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 0 :
                            record.setAuditStatusStr("地市管理员审核拒绝");
                            break;
                        case 1 :
                            record.setAuditStatusStr("地市管理员审核通过");
                            break;
                        case 2 :
                            record.setAuditStatusStr("待省公司审核");
                            break;
                    }
                    record.setStatus(elsProjectsStatusLog.getStatus());
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
                //省公司
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_ADMIN_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 0 :
                            record.setAuditStatusStr("省公司审核拒绝");
                            break;
                        case 1 :
                            record.setAuditStatusStr("审核通过");
                            break;
                    }
                    record.setStatus(elsProjectsStatusLog.getStatus());
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
            }
        }
        page.setRecords(records);
        return page;
    }

    @Override
    public IPage<ElsProjectFilesVO> paginCategoryQueryByDeptId(Integer pageNum, Integer pageSize, Integer projectCategory, Integer projectType, String projectName, String deptId) {
        Page<ElsProjects> elsProjectsPage = new Page<>(pageNum, pageSize);
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        Page<ElsProjectFilesVO> page = baseMapper.paginCategoryQueryByDeptId(elsProjectsPage,projectCategory,projectType,projectName,year,deptId);

        List<ElsProjectFilesVO> records = page.getRecords();
        for (ElsProjectFilesVO record : records) {
            //审核状态
            List<ElsProjectsStatusLog> elsProjectsStatusLogs = elsProjectsStatusLogMapper.selectList(new QueryWrapper<ElsProjectsStatusLog>().eq("project_id", record.getId()).orderByDesc("auth_time"));
            if (elsProjectsStatusLogs.size() == 0) {
                record.setAuditStatusStr("未提交");
            } else {
                ElsProjectsStatusLog elsProjectsStatusLog = elsProjectsStatusLogs.get(0);
                //地责专员
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_SPECIAL_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 2 :
                            record.setAuditStatusStr("待审核");
                            break;
                    }
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
                //地市管理员
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_SPECIAL_ADMIN_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 0 :
                            record.setAuditStatusStr("地市管理员审核拒绝");
                            break;
                        case 1 :
                            record.setAuditStatusStr("地市管理员审核通过");
                            break;
                        case 2 :
                            record.setAuditStatusStr("待省公司审核");
                            break;
                    }
                    record.setStatus(elsProjectsStatusLog.getStatus());
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
                //省公司
                if (BaseRoleUtil.hasRole(elsProjectsStatusLog.getBaseRole(), BaseRoleConstant.BASE_ADMIN_ROLE)) {
                    switch (elsProjectsStatusLog.getStatus()) {
                        case 0 :
                            record.setAuditStatusStr("省公司审核拒绝");
                            break;
                        case 1 :
                            record.setAuditStatusStr("审核通过");
                            break;
                    }
                    record.setStatus(elsProjectsStatusLog.getStatus());
                    Long statusBy = elsProjectsStatusLog.getAuthBy();
                    User user = userMapper.selectById(statusBy);
                    record.setStatusName(user.getUserName());
                    record.setStatusTime(elsProjectsStatusLog.getAuthTime());
                }
            }
        }
        page.setRecords(records);

        return page;
    }

    @Override
    public List<WorkersProjectsExcel> paginCategoryQueryByExpertExcel(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<WorkersProjectsExcel> page = baseMapper.paginCategoryQueryByExpertExcel(projectCategory,projectType,projectName,year,userId);
        return page;
    }

    @Override
    public List<WorkersProjectsExcel> paginCategoryQueryBySpecialExcel(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<WorkersProjectsExcel> page = baseMapper.paginCategoryQueryBySpecialExcel(projectCategory,projectType,projectName,year,userId);
//        for (WorkersProjectsExcel record : page) {
//            //验证通过
//            Integer integer = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_valid", 1).eq("is_deleted", 0).eq("project_id",record.getId()));
//            record.setIsValid(integer);
//            //已上传
//            Integer integer1 = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_deleted", 0).eq("project_id", record.getId()).groupBy("category_id"));
//            if (ObjectUtil.isNull(integer1)) {
//                integer1 = 0;
//            }
//            record.setUpload(integer1);
//            //未上传
//            Integer integer2 = elsProjectFilesCategoryMapper.selectCount(new QueryWrapper<ElsProjectFilesCategory>());
//            if (ObjectUtil.isNull(integer2)) {
//                integer2 = 0;
//            }
//            record.setNoUpload(integer2-integer1);
//
//            //审核状态
//            List<ElsProjectsStatusLog> elsProjectsStatusLogs = elsProjectsStatusLogMapper.selectList(new QueryWrapper<ElsProjectsStatusLog>().eq("project_id", record.getId()).eq("status_base_role", 16).orderByDesc("status_time"));
//            if (elsProjectsStatusLogs.size() == 0) {
//                record.setStatus(2);
//            } else {
//                ElsProjectsStatusLog elsProjectsStatusLog = elsProjectsStatusLogs.get(0);
//                record.setStatus(elsProjectsStatusLog.getStatus());
//                Long statusBy = elsProjectsStatusLog.getStatusBy();
//                User user = userMapper.selectById(statusBy);
//                record.setStatusName(user.getUserName());
//                record.setStatusTime(elsProjectsStatusLog.getStatusTime());
//            }
//        }
        return page;
    }

    @Override
    public List<WorkersProjectsExcel> paginCategoryQueryExcel(Integer projectCategory, Integer projectType, String projectName) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<WorkersProjectsExcel> page = baseMapper.paginCategoryQueryExcel(projectCategory,projectType,projectName,year);
        return page;
    }

    @Override
    public List<ProduceProjectsExcel> paginCategoryQueryByExpertExcelProject(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<ProduceProjectsExcel> page = baseMapper.paginCategoryQueryByExpertExcelProduct(projectCategory,projectType,projectName,year,userId);
        return page;
    }

    @Override
    public List<ProduceProjectsExcel> paginCategoryQueryBySpecialExcelProject(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<ProduceProjectsExcel> page = baseMapper.paginCategoryQueryBySpecialExcelProduce(projectCategory,projectType,projectName,year,userId);
//        for (WorkersProjectsExcel record : page) {
//            //验证通过
//            Integer integer = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_valid", 1).eq("is_deleted", 0).eq("project_id",record.getId()));
//            record.setIsValid(integer);
//            //已上传
//            Integer integer1 = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_deleted", 0).eq("project_id", record.getId()).groupBy("category_id"));
//            if (ObjectUtil.isNull(integer1)) {
//                integer1 = 0;
//            }
//            record.setUpload(integer1);
//            //未上传
//            Integer integer2 = elsProjectFilesCategoryMapper.selectCount(new QueryWrapper<ElsProjectFilesCategory>());
//            if (ObjectUtil.isNull(integer2)) {
//                integer2 = 0;
//            }
//            record.setNoUpload(integer2-integer1);
//
//            //审核状态
//            List<ElsProjectsStatusLog> elsProjectsStatusLogs = elsProjectsStatusLogMapper.selectList(new QueryWrapper<ElsProjectsStatusLog>().eq("project_id", record.getId()).eq("status_base_role", 16).orderByDesc("status_time"));
//            if (elsProjectsStatusLogs.size() == 0) {
//                record.setStatus(2);
//            } else {
//                ElsProjectsStatusLog elsProjectsStatusLog = elsProjectsStatusLogs.get(0);
//                record.setStatus(elsProjectsStatusLog.getStatus());
//                Long statusBy = elsProjectsStatusLog.getStatusBy();
//                User user = userMapper.selectById(statusBy);
//                record.setStatusName(user.getUserName());
//                record.setStatusTime(elsProjectsStatusLog.getStatusTime());
//            }
//        }
        return page;
    }

    @Override
    public List<ProduceProjectsExcel> paginCategoryQueryExcelProject(Integer projectCategory, Integer projectType, String projectName) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<ProduceProjectsExcel> page = baseMapper.paginCategoryQueryExcelProduce(projectCategory,projectType,projectName,year);
        return page;
    }

    @Override
    public List<PurchaseProjectExcel> paginCategoryQueryByExpertExcelPurchase(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<PurchaseProjectExcel> page = baseMapper.paginCategoryQueryByExpertExcelPurchase(projectCategory,projectType,projectName,year,userId);
        return page;
    }

    @Override
    public List<PurchaseProjectExcel> paginCategoryQueryBySpecialExcelPurchase(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<PurchaseProjectExcel> page = baseMapper.paginCategoryQueryBySpecialExcelPurchase(projectCategory,projectType,projectName,year,userId);
//        for (WorkersProjectsExcel record : page) {
//            //验证通过
//            Integer integer = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_valid", 1).eq("is_deleted", 0).eq("project_id",record.getId()));
//            record.setIsValid(integer);
//            //已上传
//            Integer integer1 = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_deleted", 0).eq("project_id", record.getId()).groupBy("category_id"));
//            if (ObjectUtil.isNull(integer1)) {
//                integer1 = 0;
//            }
//            record.setUpload(integer1);
//            //未上传
//            Integer integer2 = elsProjectFilesCategoryMapper.selectCount(new QueryWrapper<ElsProjectFilesCategory>());
//            if (ObjectUtil.isNull(integer2)) {
//                integer2 = 0;
//            }
//            record.setNoUpload(integer2-integer1);
//
//            //审核状态
//            List<ElsProjectsStatusLog> elsProjectsStatusLogs = elsProjectsStatusLogMapper.selectList(new QueryWrapper<ElsProjectsStatusLog>().eq("project_id", record.getId()).eq("status_base_role", 16).orderByDesc("status_time"));
//            if (elsProjectsStatusLogs.size() == 0) {
//                record.setStatus(2);
//            } else {
//                ElsProjectsStatusLog elsProjectsStatusLog = elsProjectsStatusLogs.get(0);
//                record.setStatus(elsProjectsStatusLog.getStatus());
//                Long statusBy = elsProjectsStatusLog.getStatusBy();
//                User user = userMapper.selectById(statusBy);
//                record.setStatusName(user.getUserName());
//                record.setStatusTime(elsProjectsStatusLog.getStatusTime());
//            }
//        }
        return page;
    }

    @Override
    public List<PurchaseProjectExcel> paginCategoryQueryExcelPurchase(Integer projectCategory, Integer projectType, String projectName) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<PurchaseProjectExcel> page = baseMapper.paginCategoryQueryExcelPurchase(projectCategory,projectType,projectName,year);
        return page;
    }

    @Override
    public List<ExploitProjectsExcel> paginCategoryQueryByExpertExcelExploit(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<ExploitProjectsExcel> page = baseMapper.paginCategoryQueryByExpertExcelExploit(projectCategory,projectType,projectName,year,userId);
        return page;
    }

    @Override
    public List<ExploitProjectsExcel> paginCategoryQueryBySpecialExcelExploit(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<ExploitProjectsExcel> page = baseMapper.paginCategoryQueryBySpecialExcelExploit(projectCategory,projectType,projectName,year,userId);
//        for (WorkersProjectsExcel record : page) {
//            //验证通过
//            Integer integer = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_valid", 1).eq("is_deleted", 0).eq("project_id",record.getId()));
//            record.setIsValid(integer);
//            //已上传
//            Integer integer1 = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_deleted", 0).eq("project_id", record.getId()).groupBy("category_id"));
//            if (ObjectUtil.isNull(integer1)) {
//                integer1 = 0;
//            }
//            record.setUpload(integer1);
//            //未上传
//            Integer integer2 = elsProjectFilesCategoryMapper.selectCount(new QueryWrapper<ElsProjectFilesCategory>());
//            if (ObjectUtil.isNull(integer2)) {
//                integer2 = 0;
//            }
//            record.setNoUpload(integer2-integer1);
//
//            //审核状态
//            List<ElsProjectsStatusLog> elsProjectsStatusLogs = elsProjectsStatusLogMapper.selectList(new QueryWrapper<ElsProjectsStatusLog>().eq("project_id", record.getId()).eq("status_base_role", 16).orderByDesc("status_time"));
//            if (elsProjectsStatusLogs.size() == 0) {
//                record.setStatus(2);
//            } else {
//                ElsProjectsStatusLog elsProjectsStatusLog = elsProjectsStatusLogs.get(0);
//                record.setStatus(elsProjectsStatusLog.getStatus());
//                Long statusBy = elsProjectsStatusLog.getStatusBy();
//                User user = userMapper.selectById(statusBy);
//                record.setStatusName(user.getUserName());
//                record.setStatusTime(elsProjectsStatusLog.getStatusTime());
//            }
//        }
        return page;
    }

    @Override
    public List<ExploitProjectsExcel> paginCategoryQueryExcelExploit(Integer projectCategory, Integer projectType, String projectName) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<ExploitProjectsExcel> page = baseMapper.paginCategoryQueryExcelExploit(projectCategory,projectType,projectName,year);
        return page;
    }

    @Override
    public List<PersonnelProjectsExcel> paginCategoryQueryByExpertExcelPersonnel(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<PersonnelProjectsExcel> page = baseMapper.paginCategoryQueryByExpertExcelPersonnel(projectCategory,projectType,projectName,year,userId);
        return page;
    }

    @Override
    public List<PersonnelProjectsExcel> paginCategoryQueryBySpecialExcelPersonnel(Integer projectCategory, Integer projectType, String projectName, Long userId) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<PersonnelProjectsExcel> page = baseMapper.paginCategoryQueryBySpecialExcelPersonnel(projectCategory,projectType,projectName,year,userId);
//        for (WorkersProjectsExcel record : page) {
//            //验证通过
//            Integer integer = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_valid", 1).eq("is_deleted", 0).eq("project_id",record.getId()));
//            record.setIsValid(integer);
//            //已上传
//            Integer integer1 = elsProjectFilesMapper.selectCount(new QueryWrapper<ElsProjectFiles>().eq("is_deleted", 0).eq("project_id", record.getId()).groupBy("category_id"));
//            if (ObjectUtil.isNull(integer1)) {
//                integer1 = 0;
//            }
//            record.setUpload(integer1);
//            //未上传
//            Integer integer2 = elsProjectFilesCategoryMapper.selectCount(new QueryWrapper<ElsProjectFilesCategory>());
//            if (ObjectUtil.isNull(integer2)) {
//                integer2 = 0;
//            }
//            record.setNoUpload(integer2-integer1);
//
//            //审核状态
//            List<ElsProjectsStatusLog> elsProjectsStatusLogs = elsProjectsStatusLogMapper.selectList(new QueryWrapper<ElsProjectsStatusLog>().eq("project_id", record.getId()).eq("status_base_role", 16).orderByDesc("status_time"));
//            if (elsProjectsStatusLogs.size() == 0) {
//                record.setStatus(2);
//            } else {
//                ElsProjectsStatusLog elsProjectsStatusLog = elsProjectsStatusLogs.get(0);
//                record.setStatus(elsProjectsStatusLog.getStatus());
//                Long statusBy = elsProjectsStatusLog.getStatusBy();
//                User user = userMapper.selectById(statusBy);
//                record.setStatusName(user.getUserName());
//                record.setStatusTime(elsProjectsStatusLog.getStatusTime());
//            }
//        }
        return page;
    }

    @Override
    public List<PersonnelProjectsExcel> paginCategoryQueryExcelPersonnel(Integer projectCategory, Integer projectType, String projectName) {
        Calendar calendar = Calendar.getInstance();
        Integer year=calendar.get(Calendar.YEAR);
        List<PersonnelProjectsExcel> page = baseMapper.paginCategoryQueryExcelPersonnel(projectCategory,projectType,projectName,year);
        return page;
    }


}